package com.decheng.mongo.controller;

import com.decheng.mongo.dao.UserRepository;
import com.decheng.mongo.entity.User;
import com.decheng.mongo.entity.UserQueryVo;
import com.decheng.mongo.result.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/2 12:29 下午
 * @desc
 */
@Api(tags = "MongoRepository Api 测试")
@RestController
@RequestMapping("/mongo/r")
public class MongoRepositoryController {

    private final UserRepository userRepository;

    public MongoRepositoryController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @ApiOperation("添加")
    @PostMapping("save")
    public ApiResult saveUser(@RequestBody User user) {
        return ApiResult.ok().data("item", userRepository.save(user));
    }

    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public ApiResult findAll() {
        return ApiResult.ok().data(userRepository.findAll());
    }

    @ApiOperation("根据id查询")
    @GetMapping("findById/{id}")
    public ApiResult findById(@PathVariable String id) {
        return ApiResult.ok().data("item", userRepository.findById(id).get());
    }

    @ApiOperation("条件查询")
    @PostMapping("findByQuery")
    public ApiResult findByQuery(@RequestBody UserQueryVo userQueryVo) {
        User user = new User();
        BeanUtils.copyProperties(userQueryVo, user);
        Example<User> of = Example.of(user);
        return ApiResult.ok().data(userRepository.findAll(of));

    }

    @ApiOperation("模糊查询")
    @GetMapping("findLikeName")
    public ApiResult findUsersLikeName(@RequestParam String name) {
        // 创建匹配器，即如何使用查询条件
        // 1. 构建对象
        // 2. 改变默认字符串匹配方式：模糊查询
        // 3. 改变默认大小写忽略方式：忽略大小写
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        User user = new User();
        user.setName(name);
        Example<User> userExample = Example.of(user, matcher);

        return ApiResult.ok().data(userRepository.findAll(userExample));
    }

    @ApiOperation("分页查询")
    @GetMapping("findPage/{current}/{size}")
    public ApiResult findUsersPage(@PathVariable int current, @PathVariable int size) {
        // 设置排序规则(可选)
        Sort sort = Sort.by(Sort.Direction.DESC, "age");
        // 0 为第一页
        Pageable pageable = PageRequest.of(current - 1, size, sort);
        Page<User> pages = userRepository.findAll(pageable);
        return ApiResult.ok().data(pages.getTotalElements(), pages.getContent());
    }


    @ApiOperation("修改")
    @PutMapping("update")
    public ApiResult update(@RequestBody User user) {
        return ApiResult.ok().data("item", userRepository.save(user));
    }


    @ApiOperation("删除")
    @DeleteMapping("delete/{id}")
    public ApiResult delete(@PathVariable String id) {
        userRepository.deleteById(id);
        return ApiResult.ok();
    }

    @ApiOperation("根据姓名查询")
    @GetMapping("findByName/{name}")
    public ApiResult findByName(@PathVariable String name) {
        return ApiResult.ok().data(userRepository.findByName(name));
    }

    @ApiOperation("根据姓名模糊查询")
    @GetMapping("findByNameLike/{name}")
    public ApiResult findByNameLike(@PathVariable String name) {
        return ApiResult.ok().data(userRepository.findByNameLike(name));
    }

    @ApiOperation("查询小于指定年龄的用户")
    @GetMapping("findByAgeLessThan/{age}")
    public ApiResult findByAgeLessThan(@PathVariable int age) {
        return ApiResult.ok().data(userRepository.findByAgeLessThan(age));
    }
}
